package com.ljx.offer;

import org.junit.Test;

/**
 * @author 李捷禧
 * Date: 2023/3/28
 * ClassName: C_67
 */

public class C_67 {
    /**
     * 把字符串转换成整数
     * 使用自己的暴力解法
     * @param str
     * @return
     */
    public int strToInt(String str) {
        char[] c = str.trim().toCharArray();
        if(c.length ==0) return 0;
        long num = 0;
        boolean isF = false;
        boolean isZ = false;
        if(c[0] == '-') isF =true;
        else if(c[0] == '+') isZ = true;
        else if(c[0] < '0' || c[0] > '9') return 0;
        else num = c[0] - '0';
        for (int i = 1; i < c.length; i++) {
            if(c[i] > '9' || c[i] < '0')break;
            num = num*10 +(c[i] -'0');
            if(num > Integer.MAX_VALUE) {
                if(isF) return Integer.MIN_VALUE;
                else return Integer.MAX_VALUE;
            }
        }
            if (isF) num = -num;
            return (int) num;
    }


    @Test
    public void test(){
        String str = " +5";
        System.out.println(strToInt(str));

    }

}
